//
//  OtherDetailViewModel.swift
//  ImGuider X
//
//  Created by 陆涛 on 2018/9/29.
//  Copyright © 2018 imguider. All rights reserved.
//

import UIKit

enum OtherDetailCellType {
    
    case title
    case mapImage
    case promotion
    case line
    case facility
    case optime
    case contact
    case footer
}

class OtherDetailViewModel: NSObject {
    
    var calendar:Calendar? {
        
        didSet {
            
            if let view = calendar?.viewDetail {
                
                calendar?.lat = view.coor?.latitude
                calendar?.lng = view.coor?.longitude
                
                calendar?.opentime = view.openinfo
                calendar?.website = view.website
                calendar?.telephone = view.telephone
                calendar?.address = view.address
            }
        }
    }
    
    var cellArray:[OtherDetailCellType]? {
        
        guard let _ = calendar else { return nil }
        
        var cells:[OtherDetailCellType] = []
        
        if title?.count ?? 0 > 0 {
            
            cells.append(.title)
        }
        
        if calendar?.lineDetail?.linepics?.count ?? 0 > 0 {
            cells.append(.mapImage)
        }
        
        if promotions?.count ?? 0 > 0 {
            
            cells.append(.promotion)
        }
        
        if lines?.count ?? 0 > 0 {
            
            cells.append(.line)
        }
        
        if facilities?.count ?? 0 > 0 {
            cells.append(.facility)
        }
        
        if openTime?.count ?? 0 > 0 {
            
            cells.append(.optime)
        }
        
        if contact?.length ?? 0 > 0 {
            
            cells.append(.contact)
        }
        
        if cells.count > 0 {
            
            cells.append(.footer)
        }
        
        return cells
    }
    
    var title:String? {
        
        return calendar?.title
    }
    
    var promotions:[Promotion]? {
        
        return calendar?.promotion
    }
    
    var lines:[Line]? {
        
        if let line = calendar?.lineDetail {
            
            return [line]
        }
        
        return  calendar?.viewDetail?.lines
    }
    
    var facilities:[[String:String]]? {
        
        return calendar?.facilities
    }
    
    var openTime:String? {
        
        return calendar?.opentime
    }
    
    var contact:NSAttributedString? {
        
        guard let calendar = calendar else { return nil }
        
        let att = NSMutableAttributedString()
        
        if let tel = calendar.telephone,tel.count > 0 {
            
            let imageAtt = NSAttributedString.yy_attachmentString(withEmojiImage: UIImage(named: "电话")!, fontSize: 10)
            
            att.append(imageAtt!)
            
            let subAtt = NSMutableAttributedString(string: "  \(tel)")
            
            subAtt.yy_setTextHighlight(subAtt.yy_rangeOfAll(), color: nil, backgroundColor: nil) { _,_,_,_ in
                
                Util.phoneAction(tel: tel)
            }
            
            att.append(subAtt)
        }
        
        if let address = calendar.address,address.count > 0 {
            
            if att.length > 0 {
                
                att.yy_appendString("\n")
            }
            
            let imageAtt = NSAttributedString.yy_attachmentString(withEmojiImage: UIImage(named: "地址")!, fontSize: 10)
            att.append(imageAtt!)
            
            att.yy_appendString("  \(address)")
        }
        
        if let website = calendar.website, website.count > 0 {
            
            if att.length > 0 {
                
                att.yy_appendString("\n")
            }
            
            let imageAtt = NSAttributedString.yy_attachmentString(withEmojiImage: UIImage(named: "网址")!, fontSize: 10)
            att.append(imageAtt!)
            
            let subAtt = NSMutableAttributedString(string: "  \(website)")
            
            subAtt.yy_setTextHighlight(subAtt.yy_rangeOfAll(), color: nil, backgroundColor: nil) { _,_,_,_ in
                
                Util.websiteAction(website: website)
            }
            att.append(subAtt)
        }
        
        if att.length == 0 {
            return nil
        }
        
        att.yy_font = kFont10
        att.yy_color = UIColor.hexStringToColor(hexString: "#222222")
        att.yy_lineSpacing = 8
        
        return att
    }
}

